package cn.wolfcode.business.service.Impl;

import cn.wolfcode.business.mapper.BusAppointmentMapper;
import cn.wolfcode.business.service.IBusAppointmentService;
import cn.wolfcode.business.utils.RegexUtils;
import cn.wolfcode.business.utils.VehiclePlateNoUtil;
import cn.wolfcode.common.utils.DateUtils;
import cn.wolfcode.domain.BusAppointment;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author wolfcode
 * @date 2024-08-02
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService 
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     * 需要做一个数据校验
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointment busAppointment)
    {
        //非空判断
//        if(busAppointment==null){
//            throw new RuntimeException("参数不能为空");
//        }
        Assert.notNull(busAppointment,"参数不能为空");
        //用户电话的校验
        if (!RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone())) {
            throw new RuntimeException("手机号码格式不正确");
        }
        //用户车牌号的校验
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate());
        if(vehiclePlateNo==null){
            throw new RuntimeException("车牌号格式不正确");
        }
        //日期的校验，预约日期在系统当前日期之后
        Date date=new Date();
        if(date.after(busAppointment.getAppointmentTime())){
            throw new RuntimeException("预约时间应在系统当前时间之后");
        }
        //为了防止用户传递的数据改变系统的默认值
        BusAppointment busAppointment1 = new BusAppointment();
        busAppointment.setCreateTime(new Date());
        BeanUtils.copyProperties(busAppointment,busAppointment1);
        busAppointment.setCreateTime(DateUtils.getNowDate());
        return busAppointmentMapper.insertBusAppointment(busAppointment1);
    }

    /**
     * 修改养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointment busAppointment)
    {
        //非空判断
//        if(busAppointment==null){
//            throw new RuntimeException("参数不能为空");
//        }
        Assert.notNull(busAppointment,"参数不能为空");
        //用户电话的校验
        if (!RegexUtils.isPhoneLegal(busAppointment.getCustomerPhone())) {
            throw new RuntimeException("手机号码格式不正确");
        }
        //用户车牌号的校验
        VehiclePlateNoUtil.VehiclePlateNoEnum vehiclePlateNo = VehiclePlateNoUtil.getVehiclePlateNo(busAppointment.getLicensePlate());
        if(vehiclePlateNo==null){
            throw new RuntimeException("车牌号格式不正确");
        }
        //日期的校验，预约日期在系统当前日期之后
        Date date=new Date();
        if(date.after(busAppointment.getAppointmentTime())){
            throw new RuntimeException("预约时间应在系统当前时间之后");
        }
        //为了防止用户传递的数据改变系统的默认值
        BusAppointment busAppointment1 = new BusAppointment();
        busAppointment.setCreateTime(new Date());
        BeanUtils.copyProperties(busAppointment,busAppointment1);
        busAppointment.setCreateTime(DateUtils.getNowDate());
        return busAppointmentMapper.updateBusAppointment(busAppointment1);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids)
    {
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     * 
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }
}
